package com.example.retrofit2demo;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Button button;
    private TextView textViewDay;
    private TextView textViewCurrent;
    private ImageView imageView;
    private LinearLayout linearLayout;

    private Retrofit retrofit;
    private RequestWeatherinfo requestWeatherinfo;
    private Retrofit retrofit2;
    private RequestCityCode requestCityCode;
    private List<City> cityList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=findViewById(R.id.editText);
        button=findViewById(R.id.button);
        textViewDay=findViewById(R.id.textViewDay);
        textViewCurrent=findViewById(R.id.textViewCurrent);
        imageView=findViewById(R.id.imageView);
        linearLayout=findViewById(R.id.linearLayout);
        createRetrofit();
        getDayWeatherByCityid("101180101");
        createRetrofit2();
        getAllCityCode();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String cityname=editText.getText().toString();
                String cityid="101180101";
                for(City city:cityList)
                {
                    if(cityname.equals(city.getCity_name()))
                    {
                        cityid=city.getCity_code();
                    }
                }
                getDayWeatherByCityid(cityid);
                getCurrentWeatherByCityid(cityid);
            }
        });
    }

    //初始化Retrofit对象
    private void createRetrofit()
    {
        retrofit=new Retrofit.Builder()
                .baseUrl("http://www.weather.com.cn/data/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        requestWeatherinfo=retrofit.create(RequestWeatherinfo.class);
    }

    private void createRetrofit2()
    {
        retrofit2=new Retrofit.Builder()
                .baseUrl("http://172.28.22.100/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        requestCityCode=retrofit2.create(RequestCityCode.class);
    }

    private void getAllCityCode()
    {
        Call<List<City>> call=requestCityCode.getCityCodes();
        call.enqueue(new Callback<List<City>>() {
            @Override
            public void onResponse(Call<List<City>> call, Response<List<City>> response) {
                cityList=response.body();
            }

            @Override
            public void onFailure(Call<List<City>> call, Throwable t) {

            }
        });
    }


    //请求DayWeaher
    private void getDayWeatherByCityid(String cityid)
    {
        Call<DayWeather> call=requestWeatherinfo.getDayWeather(cityid);
        call.enqueue(new Callback<DayWeather>() {
            @Override
            public void onResponse(Call<DayWeather> call, Response<DayWeather> response) {
                DayWeather.WeatherinfoBean weatherinfoBean=response.body().getWeatherinfo();
                textViewDay.setText(weatherinfoBean.getCity()
                        +":"+weatherinfoBean.getWeather()+weatherinfoBean.getTemp1()+"~"+weatherinfoBean.getTemp2());
                if(weatherinfoBean.getWeather().equals("多云"))
                {
                    Glide.with(MainActivity.this).load("http://172.28.22.100/drawable/clouds.png").into(imageView);
                    Glide.with(MainActivity.this).load("http://172.28.22.100/drawable/bz1.jpg").into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            linearLayout.setBackground(resource);
                        }
                    });
                }
                if(weatherinfoBean.getWeather().equals("阴转晴"))
                {
                    Glide.with(MainActivity.this).load("http://172.28.22.100/drawable/cloud_sun.png").into(imageView);
                    Glide.with(MainActivity.this).load("http://172.28.22.100/drawable/bz2.png").into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            linearLayout.setBackground(resource);
                        }
                    });
                }
                if(weatherinfoBean.getWeather().equals("晴"))
                {
                    Glide.with(MainActivity.this).load("http://172.28.22.100/drawable/sun.png").into(imageView);
                    Glide.with(MainActivity.this).load("http://172.28.22.100/drawable/bz2.png").into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            linearLayout.setBackground(resource);
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<DayWeather> call, Throwable t) {
                textViewDay.setText(t.getMessage());
            }
        });

    }

    //请求CurrentWeather
    private void getCurrentWeatherByCityid(String cityid)
    {

    }
}
